package com.meihua.shellapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.utils.ConstantUtils;

/**
 * 一级滑动
 * 作者：lc on 2017/10/14.
 * 邮箱：lc0607live@163.com
 */

public abstract class SlideListDialog extends Dialog implements View.OnClickListener {

    private LinearLayout ll_dialog;//布局总id

    private NumberPicker np_one;//省
    private TextView tv_cencle;//取消
    private TextView tv_sure;//确定

    private String[] value;


    public SlideListDialog(@NonNull Context context, String[] value) {
        super(context, R.style.MyDialog);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.BOTTOM;
        lp.width = ConstantUtils.getInstance().getScreenW(context);
        getWindow().setAttributes(lp);
        this.value = value;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_slidelist);
        initView();
    }

    /**
     * 加载布局
     */
    private void initView() {
        np_one = findViewById(R.id.np_one);
        tv_cencle = findViewById(R.id.tv_cencle);
        tv_sure = findViewById(R.id.tv_sure);
        ll_dialog = findViewById(R.id.ll_dialog);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ll_dialog.getLayoutParams();
        lp.width = ConstantUtils.getInstance().getScreenW(getContext());
        ll_dialog.setLayoutParams(lp);
        tv_sure.setOnClickListener(this);
        tv_cencle.setOnClickListener(this);
        bindData();
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        np_one.setMinValue(0);
        np_one.setMaxValue(value.length - 1);
        np_one.setDisplayedValues(value);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cencle://取消
                dismiss();
                break;
            case R.id.tv_sure://确定
                sureData(value[np_one.getValue()], np_one.getValue());
                dismiss();
                break;
        }
    }

    public abstract void sureData(String value, int selectIndex);

}
